/****************************************************************************/
/*	Function implementation - global ('extern') and local('static')
****************************************************************************/
#include "cms8s6990.h"
/******************************************************************************
 ** \brief	 UART0_Config
 ** \param [in] 
 **            	
 ** \return  none
 ** \note  
  ******************************************************************************/
void UART0_Config(void)
{
#if 1		//Ê¹ÓÃBRT×÷ÎªUARTÄ£¿éµÄ²¨ÌØÂÊÊ±ÖÓ·¢ÉúÆ÷
	 uint16_t  BRTValue = 0;
	 uint32_t  BaudRateVlue = 9600;
	 
	 /*
	 (1)ÉèÖÃUARTxµÄÔËÐÐÄ£Ê½
	 */
	 UART_ConfigRunMode(UART0,UART_MOD_ASY_8BIT, UART_BAUD_BRT);
	 UART_EnableReceive(UART0);
	 /*
	 (2)ÅäÖÃUARTxµÄ²¨ÌØÂÊ
	 */
	 UART_ConfigBRTClk(BRT_CLK_DIV_1);	
	
//	 UART_EnableDoubleFrequency(UART0); 							/*²¨ÌØÂÊÊ¹ÄÜ±¶Æµ£ºSMOD =1*/
		PCON &= 0x7F;               							//SMOD0 = 0
	
  #ifdef USE_FORMULA			//Ê¹ÓÃ¹«Ê½¼ÆËã¶¨Ê±Æ÷µÄ¼ÓÔØÖµ(ÐèÒª¶ÔSystemclock¸³Öµ(main.c))£¬USE_FORMULA ÔÚ Ñ¡ÏîOption->C51->Preporcessor Symbols->DefineÖÐ¶¨Òå
	 BRTValue = UART_ConfigBaudRate(UART0, BaudRateVlue) ;
  #else 
	 BRTValue = 65510; 				//Ê¹ÓÃÊý¾ÝÊÖ²áÉÏÍÆ¼öµÄ¼ÓÔØÖµ(BRTÕÂ½Ú)
  #endif
 
	 UART_ConfigBRTPeriod(BRTValue);						/*ÅäÖÃÖØ×°Öµ*/
	 UART_EnableBRT();										/*Ê¹ÄÜ¶¨Ê±Æ÷*/
	 /*
	 (3)ÅäÖÃIO¿Ú
	 */
	 GPIO_SET_MUX_MODE(P23CFG,GPIO_MUX_TXD0);			/*TXD0*/
	 GPIO_SET_MUX_MODE(P22CFG,GPIO_MUX_RXD0);	 		/*RXD0*/	
	 
	 //设置中断优先级
	 UART_EnableInt(UART0);
	 IRQ_SET_PRIORITY(IRQ_UART0,IRQ_PRIORITY_HIGH);
	 IRQ_ALL_ENABLE();
	 
#elif 0	 //Ê¹ÓÃTMR1×÷ÎªUARTÄ£¿éµÄ²¨ÌØÂÊÊ±ÖÓ·¢ÉúÆ÷

	 uint8_t  THxValue = 0;
	 uint32_t  BaudRateVlue = 9600;	 
	/*
	 (1)ÉèÖÃUARTxµÄÔËÐÐÄ£Ê½
	 */
	 UART_ConfigRunMode(UART0,UART_MOD_ASY_8BIT, UART_BAUD_TMR1);
	 UART_EnableReceive(UART0);
	/*
	 (2)ÅäÖÃUARTxµÄ²¨ÌØÂÊ
	 */
	 TMR_ConfigTimerClk(TMR1,TMR_CLK_DIV_4);							/*TM =1*/
	 TMR_ConfigRunMode(TMR1, TMR_MODE_TIMING, TMR_TIM_AUTO_8BIT); 		/*ÉèÖÃÎª8Î»ÖØ×°Ä£Ê½*/
	
	 UART_EnableDoubleFrequency(UART0); 							/*²¨ÌØÂÊÊ¹ÄÜ±¶Æµ£ºSMOD =1*/
	
  #ifdef USE_FORMULA			//Ê¹ÓÃ¹«Ê½¼ÆËã¶¨Ê±Æ÷µÄ¼ÓÔØÖµ(ÐèÒª¶ÔSystemclock¸³Öµ(main.c))£¬USE_FORMULA ÔÚ Ñ¡ÏîOption->C51->Preporcessor Symbols->DefineÖÐ¶¨Òå
	 THxValue = UART_ConfigBaudRate(UART0, BaudRateVlue) ;
  #else 
	 THxValue = 217; 				//Ê¹ÓÃÊý¾ÝÊÖ²áÉÏÍÆ¼öµÄ¼ÓÔØÖµ(UARTÕÂ½Ú)
  #endif
  
	 TMR_ConfigTimerPeriod(TMR1, THxValue, THxValue);			/*ÅäÖÃÖØ×°Öµ*/
	 TMR_Start(TMR1);											/*Ê¹ÄÜ¶¨Ê±Æ÷*/
	
	 /*
	 (3)ÅäÖÃIO¿Ú
	 */
	 GPIO_SET_MUX_MODE(P23CFG,GPIO_MUX_TXD0);			/*TXD0*/
	 GPIO_SET_MUX_MODE(P22CFG,GPIO_MUX_RXD0);	 		/*RXD0*/

#elif 0		//Ê¹ÓÃTMR4×÷ÎªUARTÄ£¿éµÄ²¨ÌØÂÊÊ±ÖÓ·¢ÉúÆ÷ 

	 uint8_t  THxValue = 0;
	 uint32_t  BaudRateVlue = 9600;	 
	/*
	 (1)ÉèÖÃUARTxµÄÔËÐÐÄ£Ê½
	 */
	 UART_ConfigRunMode(UART0,UART_MOD_ASY_8BIT, UART_BAUD_TMR4);
	 UART_EnableReceive(UART0);
	/*
	 (2)ÅäÖÃUARTxµÄ²¨ÌØÂÊ
	 */
	 TMR_ConfigTimerClk(TMR4,TMR_CLK_DIV_4);							/*TM =1*/
	 TMR_ConfigRunMode(TMR4, TMR_MODE_TIMING, TMR_TIM_AUTO_8BIT); 		/*ÉèÖÃÎª8Î»ÖØ×°Ä£Ê½*/
	
	 UART_EnableDoubleFrequency(UART0); 							/*²¨ÌØÂÊÊ¹ÄÜ±¶Æµ£ºSMOD =1*/
	
  #ifdef USE_FORMULA			//Ê¹ÓÃ¹«Ê½¼ÆËã¶¨Ê±Æ÷µÄ¼ÓÔØÖµ(ÐèÒª¶ÔSystemclock¸³Öµ(main.c))£¬USE_FORMULA ÔÚ Ñ¡ÏîOption->C51->Preporcessor Symbols->DefineÖÐ¶¨Òå
	 THxValue = UART_ConfigBaudRate(UART0, BaudRateVlue) ;
  #else 
	 THxValue = 217; 				//Ê¹ÓÃÊý¾ÝÊÖ²áÉÏÍÆ¼öµÄ¼ÓÔØÖµ(UARTÕÂ½Ú)
  #endif
  
	 TMR_ConfigTimerPeriod(TMR4, THxValue, THxValue);			/*ÅäÖÃÖØ×°Öµ*/
	 TMR_Start(TMR4);											/*Ê¹ÄÜ¶¨Ê±Æ÷*/
	
	 /*
	 (3)ÅäÖÃIO¿Ú
	 */
	 GPIO_SET_MUX_MODE(P23CFG,GPIO_MUX_TXD0);			/*TXD0*/
	 GPIO_SET_MUX_MODE(P22CFG,GPIO_MUX_RXD0);	 		/*RXD0*/


#elif  0		//Ê¹ÓÃTMR2×÷ÎªUARTÄ£¿éµÄ²¨ÌØÂÊÊ±ÖÓ·¢ÉúÆ÷	 
	 uint16_t  TMR2Value = 0;
	 uint32_t  BaudRateVlue = 9600;	 
	/*
	 (1)ÉèÖÃUARTxµÄÔËÐÐÄ£Ê½
	 */
	 UART_ConfigRunMode(UART0,UART_MOD_ASY_8BIT, UART_BAUD_TMR2);
	 UART_EnableReceive(UART0);
	/*
	 (2)ÅäÖÃUARTxµÄ²¨ÌØÂÊ
	 */
	 TMR2_ConfigTimerClk(TMR2_CLK_DIV_12);						/*T2PS =0*/
	 TMR2_ConfigRunMode(TMR2_MODE_TIMING, TMR2_AUTO_LOAD); 		/*ÉèÖÃÎª8Î»ÖØ×°Ä£Ê½*/
	
	 UART_EnableDoubleFrequency(UART0); 							/*²¨ÌØÂÊÊ¹ÄÜ±¶Æµ£ºSMOD =1*/
	
  #ifdef USE_FORMULA				//Ê¹ÓÃ¹«Ê½¼ÆËã¶¨Ê±Æ÷µÄ¼ÓÔØÖµ(ÐèÒª¶ÔSystemclock¸³Öµ(main.c))£¬USE_FORMULA ÔÚ Ñ¡ÏîOption->C51->Preporcessor Symbols->DefineÖÐ¶¨Òå
	 TMR2Value = UART_ConfigBaudRate(UART0, BaudRateVlue) ;
  #else 
	 TMR2Value = 65523; 				
  #endif
  
	 TMR2_ConfigTimerPeriod(TMR2Value);						/*ÅäÖÃÖØ×°Öµ*/
	 TMR2_Start();											/*Ê¹ÄÜ¶¨Ê±Æ÷*/
	
	 /*
	 (3)ÅäÖÃIO¿Ú
	 */
	 GPIO_SET_MUX_MODE(P23CFG,GPIO_MUX_TXD0);			/*TXD0*/
	 GPIO_SET_MUX_MODE(P22CFG,GPIO_MUX_RXD0);	 		/*RXD0*/

#endif
}



/******************************************************************************
 ** \brief	 putchar
 ** \param [in] data
 **            	
 ** \return  none
 ** \note   <stdio.h>ÖÐÐèÒªµÄº¯Êý
 ******************************************************************************/
char putchar (char ch)
{
	SBUF0 = ch;
	while( !(SCON0 & (1<<1)));
	SCON0 &=~(1<<1);		
	return 0;
}

/******************************************************************************
 ** \brief	 putchar
 ** \param [in] none
 **            	
 ** \return  data
 ** \note   <stdio.h>ÖÐÐèÒªµÄº¯Êý
 ******************************************************************************/
char getchar (void)
{
	while(!(SCON0 & (1<<0)));
	SCON0 &=~(1<<0);
	return  SBUF0;	
}
/********************************************************************************
 ** \brief	 puts 
 **
 ** \param [in]  bytes addr for sending
 **
 ** \return  <stdio.h>ÖÐÐèÒªµÄº¯Êý
 ******************************************************************************/
int  puts( const char  * s)
{
	while(*(s ++))
		putchar(*s);
	return 0;
}
